package com.copa.ums.modules.ums.controller;

import com.copa.ums.common.api.CommonPage;
import com.copa.ums.common.vo.ResponseVo;
import com.copa.ums.modules.ums.model.UmsMenu;
import com.copa.ums.modules.ums.service.UmsMenuService;
import com.copa.ums.modules.ums.vo.UmsMenuNodeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author copa
 * @since 2022-10-31
 */
@RestController
@Api(tags = "菜单管理")
@Tag(name = "菜单管理", description = "菜单管理")
@RequestMapping("menu")
public class UmsMenuController {

    @Resource
    private UmsMenuService menuService;

    @ApiOperation("添加菜单")
    @PostMapping("create")
    public ResponseVo<String> create(@RequestBody UmsMenu menu) {
        return menuService.create(menu);
    }

    @ApiOperation("修改菜单")
    @PostMapping("update/{menuId}")
    public ResponseVo<String> update(@PathVariable("menuId") Long menuId,
                                     @RequestBody UmsMenu menu) {
        return menuService.update(menuId, menu);
    }

    @ApiOperation("根据id获取菜单详情")
    @GetMapping("{menuId}")
    public ResponseVo<UmsMenu> getItem(@PathVariable("menuId") Long menuId) {
        return ResponseVo.success(menuService.getById(menuId));
    }

    @ApiOperation("根据ID删除后台菜单")
    @PostMapping("delete/{menuId}")
    public ResponseVo<String> delete(@PathVariable("menuId") Long menuId) {
        return menuService.removeById(menuId)
                ? ResponseVo.success(null)
                : ResponseVo.failed();
    }

    @ApiOperation("分页查询菜单")
    @GetMapping("list/{parentId}")
    public ResponseVo<CommonPage<UmsMenu>> list(@PathVariable Long parentId,
                                                @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        return menuService.list(parentId, pageSize, pageNum);
    }

    @ApiOperation("树形结构返回所有菜单列表")
    @GetMapping("treeList")
    public ResponseVo<List<UmsMenuNodeVo>> treeList() {
        return menuService.treeList();
    }

    @ApiOperation("修改菜单显示状态")
    @PostMapping("updateHidden/{menuId}")
    public ResponseVo<String> updateHidden(@PathVariable("menuId") Long menuId,
                                           @RequestParam("hidden") Integer hidden) {
        return menuService.updateHidden(menuId, hidden);
    }
}

